'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT,TINYINT, DATE } = app.Sequelize;

  const Position = app.model.define('positions', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: STRING(255),
      allowNull: false,
      comment: '岗位名称'
    },
    parent_id: {
      type: INTEGER,
      allowNull: true,
      references: {
        model: 'positions',
        key: 'id'
      },
      comment: '父级岗位ID'
    },
    description: {
      type: STRING(255),
      allowNull: true,
      comment: '岗位描述'
    },
    sort: {
      type: INTEGER,
      allowNull: true,
      comment: '排序值'
    },
    status: {
      type: TINYINT,
      allowNull: true,
      comment: '状态'
    },
    icon: {
      type: STRING(255),
      allowNull: true,
      comment: '图标'
    },
    created_at: DATE,
    updated_at: DATE,
  }, {
    tableName: 'positions',
    timestamps: true,
  });

  // 定义自关联关系
  Position.associate = function() {
    app.model.Position.belongsTo(app.model.Position, { as: 'parent', foreignKey: 'parent_id' });
    app.model.Position.hasMany(app.model.Position, { as: 'children', foreignKey: 'parent_id' });
  };

  return Position;
};
